<?php
namespace Home\Controller;
use Think\Controller;
class ProductController extends Controller {
	public function detail($id){
		$product=M("product")->where('id ='.$id)->find();
		$product_imgs=M("product_img")->where('product_id = '.$id)->limit(4)->select();
		$this->assign('product',$product);
		$this->assign('product_imgs',$product_imgs);
		$this->display();
	}
	public function lists($cataid){
		$brand_id = I('get.brand');
		$catalog=M("product_catalog")->where('id ='.$cataid)->find();
		$parent_id=$catalog['parent_id'];
		$children_catalog=M("product_catalog")->where('parent_id ='.$cataid)->select();
		if ($parent_id==0){
			$this->redirect('/Home/Product/lists/cataid/'.$children_catalog[0]['id']);
		}
		$parent_catalog=M("product_catalog")->where('id ='.$parent_id)->find();
		$brother_catalogs=M("product_catalog")->where('parent_id ='.$parent_id)->select();
		$catalogs = array($cataid);
		foreach ($children_catalog as $c) {
			$catalogs[]=$c['id'];
		}
		$where='cata_id in ('.implode(',',$catalogs).')';
		if ($brand_id){
			$where = $where.' and brand_id='.$brand_id;
		}
		$products=M("product")->where($where)->select();
		$temp=M("product")->distinct(true)->where('cata_id in ('.implode(',',$catalogs).')')->field('brand_id')->select();
		$brand_ids = array();
		foreach ($temp as $t) {
			$brand_ids[]=$t['brand_id'];
		}
		$brands=M("product_brand")->where('id in ('.implode(',',$brand_ids).')')->select();
		$this->assign('catalog',$catalog);
		$this->assign('parent_catalog',$parent_catalog);
		$this->assign('brother_catalogs',$brother_catalogs);
		$this->assign('products',$products);
		$this->assign('brands',$brands);
		$this->assign('brand_id',$brand_id);
		$this->display();
	}
}